﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TaoLeH.Models.Dto;

namespace TaoLeH.WebApi
{
     /// <summary>
     /// 方法代理工厂
     /// 在程序启动时将各个服务的处理方法注册到内存中
     /// 然后在调用时根据字典获取方法对象进行执行
     /// 作者：leev
     /// 时间：2018-12-18
     /// </summary>
    public class DelegateFactory
    {
        public delegate void DoAction(ApiContext context);

        public static Dictionary<string, DoAction> Dic;

        public static DoAction GetDoAction(string methodName)
        {
            methodName = methodName.ToLower();
            DoAction action;
            if (Dic.TryGetValue(methodName, out action))
            {
                return action;
            }
            return null;
        }

        public static void SetDoAction(string methodName, Action<ApiContext> act)
        {
            SetMethod(methodName, new DoAction(act));
        }

        public static void SetMethod(string methodName, DoAction act)
        {
            methodName = methodName.ToLower();
            if (Dic == null)
            {
                Dic = new Dictionary<string, DoAction>();
            }
            if (Dic.ContainsKey(methodName))
                Dic[methodName] = act;
            else
                Dic.Add(methodName, act);
        }
    }
}
